﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Controls.Classes;
using Silvermoon.Animations;
using System.Drawing;
using System.Xml;

namespace FileExplorer
{
    public class GridWindow : Window
    {
        private Toolbar toolbar;
        private BackgroundDecorator background;

        public GridWindow()
            : base()
        {
            this.Flags = ControlFlags.IgnoreChildClipping;
            background = new BackgroundDecorator { Color = System.Drawing.Color.Silver };
            background.Margin = new Margin(0);
            Controls.Add(background);

            TextBox txtstone = new TextBox
                                    {
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8 * 3
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                                        ,
                                        Location = new Point(0, 100)
                                    };
            Controls.Add(txtstone);
            Label lblstone = new Label
                                    {
                                        Text = "st"
                                        ,
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8
                                        ,
                                        Location = new Point(Screen.Size.Width / 8 * 3, 100)
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                                    };
            Controls.Add(lblstone);
            TextBox txtpounds = new TextBox
                                    {
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8 * 3
                                        ,
                                        Location = new Point(Screen.Size.Width / 2, 100)
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))

                                    };
            Controls.Add(txtpounds);
            Label lblpounds = new Label
                                    {
                                        Text = "lb"
                                        ,
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8
                                        ,
                                        Location = new Point(Screen.Size.Width / 8 * 7, 100)
                                        ,
                                        TextColor = SysColors.TextColor
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                                    };
            Controls.Add(lblpounds);
            TextBox txtDateTime = new TextBox
                                        {
                                           Alpha = 128
                                           ,
                                           ToolTip = new DatePickerToolTip()
                                           ,
                                           Size = new Size(Screen.Width, 100)
                                           ,
                                           Location = new Point(0, 0)
                                        };
            Controls.Add(txtDateTime);
            Button btnSubmit = new Button
                                    {
                                        Text = "Submit"
                                        ,
                                        Color = SysColors.ButtonColor
                                        ,
                                        Size = new Size(300, 100)
                                        ,
                                        Location = new Point(Screen.Size.Width/2 - 150, 200)
                                    };
            Controls.Add(btnSubmit);
            btnSubmit.Click += (s, e) =>
                                   {
                                       string sFileName = "Weight.xml";
                                       XmlDocument xmlDoc = new XmlDocument();
                                       XmlNode oRootNode;
                                       XmlNode oWeightsNode;
                                       try
                                       {
                                           xmlDoc.Load(sFileName);
                                           oRootNode = xmlDoc.DocumentElement;
                                       }
                                       catch (Exception)
                                       {
                                           oRootNode = xmlDoc.CreateNode(XmlNodeType.Element, "MobileLife", "");
                                           xmlDoc.AppendChild(oRootNode);
                                           oWeightsNode = xmlDoc.CreateNode(XmlNodeType.Element, "Weight", "");
                                           oRootNode.AppendChild(oWeightsNode);

                                       }
                                       XmlNode WeightNode = oRootNode.SelectSingleNode("//Weight");

                                       XmlNode ReadingNode = xmlDoc.CreateNode(XmlNodeType.Element, "Measurement", "");
                                       if (ReadingNode.Attributes["Stones"] == null)
                                       {
                                           XmlAttribute xmlatt = xmlDoc.CreateAttribute("Stones");
                                           ReadingNode.Attributes.Append(xmlatt);
                                      } 
                                       if (ReadingNode.Attributes["Pounds"] == null)
                                       {
                                           XmlAttribute xmlatt = xmlDoc.CreateAttribute("Pounds");
                                           ReadingNode.Attributes.Append(xmlatt);
                                       }
                                       if (ReadingNode.Attributes["DateTime"] == null)
                                       {
                                           XmlAttribute xmlatt = xmlDoc.CreateAttribute("DateTime");
                                           ReadingNode.Attributes.Append(xmlatt);
                                       }
                                       ReadingNode.Attributes["DateTime"].Value = DateTime.Now.ToString();
                                       ReadingNode.Attributes["Stones"].Value = txtstone.Text;
                                       ReadingNode.Attributes["Pounds"].Value = txtpounds.Text;
                                       WeightNode.AppendChild(ReadingNode);
                                       xmlDoc.Save(sFileName);
                                       txtpounds.Text = "";
                                       txtstone.Text = "";
                                   };

            toolbar = new Toolbar();
            Toolbar = toolbar;
            toolbar.LeftButton.Text = "Exit";
            toolbar.LeftButton.Click += (s, e) => { Hide(TransitionMode.ZoomOut); };
        }
    }
}
